//
//  LoadingViewController.m
//  SheShiDu
//
//  Created by 泉  姜 on 16/6/22.
//  Copyright © 2016年 单剑秋. All rights reserved.
//

#import "LoadingViewController.h"
#import "RegisterViewController.h"
#import "PersonalViewController.h"

@interface LoadingViewController ()<UITextFieldDelegate>

@end

@implementation LoadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:234 / 255.0 blue:234 / 255.0 alpha:1];
    _i = 1;
    [self setView];
}

- (void)setView
{
    __weak typeof (self) weakSelf = self;

    self.nameTF = [[UITextField alloc]init];
    _nameTF.delegate = self;
    _nameTF.borderStyle = UITextBorderStyleRoundedRect;
    _nameTF.placeholder = @"请输入用户名";
    _nameTF.leftViewMode = UITextFieldViewModeAlways;
    _nameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    UIImageView *leftImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"per"]];
    leftImageV.frame = CGRectMake(5, 0, 22, 22);
    _nameTF.leftView = leftImageV;
    [self.view addSubview:_nameTF];
    [_nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(40);
        make.right.mas_equalTo(weakSelf.view.mas_right).offset(-40);
        make.centerY.mas_equalTo(weakSelf.view.mas_centerY).offset(-45);
    }];
    
    self.codeTF = [[UITextField alloc]init];
    _codeTF.delegate = self;
    _codeTF.borderStyle = UITextBorderStyleRoundedRect;
    _codeTF.placeholder = @"请输入密码";
    _codeTF.leftViewMode = UITextFieldViewModeAlways;
    _codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTF.secureTextEntry = YES;
    UIImageView *leftIV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"key"]];
    leftIV.frame = CGRectMake(5, 0, 22, 22);
    _codeTF.leftView = leftIV;
    
    [self.view addSubview:_codeTF];
    [_codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(40);
        make.right.mas_equalTo(weakSelf.view.mas_right).offset(-40);
        make.centerY.mas_equalTo(weakSelf.view.mas_centerY).offset(5);
    }];
    
    self.joinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _joinBtn.layer.masksToBounds = YES;
    _joinBtn.layer.cornerRadius = 5;
    [_joinBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [_joinBtn addTarget:self action:@selector(joinBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _joinBtn.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:195 / 255.0 blue:183 / 255.0 alpha:1];
    [self.view addSubview:_joinBtn];
    [_joinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(40);
        make.right.mas_equalTo(weakSelf.view.mas_right).offset(-40);
        make.centerY.mas_equalTo(weakSelf.view.mas_centerY).offset(65);
    }];
    
    self.regBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_regBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_regBtn addTarget:self action:@selector(regBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _regBtn.titleLabel.font =[UIFont systemFontOfSize:14];
    [_regBtn setTitleColor:[UIColor colorWithRed:35 / 255.0 green:195 / 255.0 blue:183 / 255.0 alpha:1] forState:UIControlStateNormal];
    [self.view addSubview:_regBtn];
    [_regBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake( 30, 30));
        make.right.mas_equalTo(_joinBtn).offset(-5);
        make.top.equalTo(_joinBtn.mas_bottom);
    }];
    
    UILabel *regLabel = [[UILabel alloc]init];
    regLabel.text = @"竟然还没有设饰度帐号";
    regLabel.textAlignment = NSTextAlignmentRight;
    regLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:regLabel];
    [regLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(150, 30));
        make.top.equalTo(_regBtn);
        make.right.equalTo(_regBtn.mas_left);
    }];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"cha"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(22, 22));
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(20);
        make.top.mas_equalTo(weakSelf.view.mas_top).offset(20);
    }];
    
}

- (void)backBtnAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)joinBtnAction:(UIButton *)btn
{
    WEAKSELF(weakSelf);
    if (_nameTF.text == [_infoDic objectForKey:@"name"] && _codeTF.text == [_infoDic objectForKey:@"code"]) {
        [self.delegate passValue:_i];
//        NSLog(@"%ld-----", _i);
        weakSelf.reBackMe(_infoDic);
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户或密码不正确" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
        [weakSelf presentViewController:alert animated:YES completion:^{
            
        }];

    }
    
}

- (void)regBtnAction:(UIButton *)btn
{
    void (^reBack) (NSMutableDictionary *dic) = ^(NSMutableDictionary *dic) {
        self.infoDic = [NSMutableDictionary dictionaryWithDictionary:dic];
        _nameTF.text = [_infoDic objectForKey:@"name"];
    };
    RegisterViewController *regVC = [[RegisterViewController alloc]init];
    regVC.reBack = reBack;
    [self presentViewController:regVC animated:YES completion:^{
       
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_nameTF resignFirstResponder];
    [_codeTF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
